package com.bjpowernode.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.bjpowernode.common.constant.Constant;
import com.bjpowernode.entity.TTran;
import com.bjpowernode.entity.query.BaseQuery;
import com.bjpowernode.entity.query.TranSaveQuery;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.service.TranService;
import com.bjpowernode.util.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class TranServiceImpl implements TranService {

    private final TTranMapper tranMapper;


    @Override
    public int createTran(TranSaveQuery tranSaveQuery) {
        TTran tran = new TTran();
        BeanUtil.copyProperties(tranSaveQuery, tran);
        tran.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        tran.setCreateTime(new Date());
        tran.setTranNo(String.valueOf(IdUtil.getSnowflakeNextId()));
        return tranMapper.insert(tran);
    }

    @Override
    public PageInfo<TTran> getTransByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);

        return new PageInfo<>(tranMapper.getTransByPage(new BaseQuery()));
    }

    @Override
    public TTran tranDetail(Integer id) {
        return tranMapper.getTranById(id);
    }
}
